#!/bin/bash

sudo ./system_backup.sh

if [ -f /etc/X11/xorg.conf.d/40-libinput.conf ]; then
sudo rm -rf /etc/X11/xorg.conf.d/40-libinput.conf
fi
if [ ! -d /etc/X11/xorg.conf.d ]; then
sudo mkdir -p /etc/X11/xorg.conf.d
fi
sudo cp ./usr/tft9341-overlay.dtb /boot/overlays/
sudo cp ./usr/tft9341-overlay.dtb /boot/overlays/tft9341.dtbo
#root_dev=`grep -oPr "root=[^\s]*" /boot/cmdline.txt | awk -F= '{printf $NF}'`
#if test "$root_dev" = "/dev/mmcblk0p7";then
#sudo cp -rf ./boot/config-noobs-nomal.txt ./boot/config.txt.bak
#else
#sudo cp -rf ./boot/config-nomal.txt ./boot/config.txt.bak
#sudo echo "hdmi_force_hotplug=1" >> ./boot/config.txt.bak
#fi

source ./system_config.sh
#sudo sed -i -e 's/dtoverlay=vc4-kms-v3d/#dtoverlay=vc4-kms-v3d/' ./boot/config.txt.bak
sudo echo "hdmi_force_hotplug=1" >> ./boot/config.txt.bak
sudo echo "dtparam=i2c_arm=on" >> ./boot/config.txt.bak
sudo echo "dtparam=spi=on" >> ./boot/config.txt.bak
sudo echo "enable_uart=1" >> ./boot/config.txt.bak
sudo echo "dtoverlay=tft9341:rotate=270" >> ./boot/config.txt.bak
sudo echo "hdmi_group=2" >> ./boot/config.txt.bak
sudo echo "hdmi_mode=1" >> ./boot/config.txt.bak
sudo echo "hdmi_mode=87" >> ./boot/config.txt.bak
sudo echo "hdmi_cvt 480 360 60 6 0 0 0" >> ./boot/config.txt.bak
sudo echo "hdmi_drive=2" >> ./boot/config.txt.bak
sudo cp -rf ./boot/config.txt.bak /boot/config.txt
sudo cp -rf ./usr/99-calibration.conf-32-270  /etc/X11/xorg.conf.d/99-calibration.conf
if [[ "$deb_version" < "12.1" ]]; then
sudo cp -rf ./usr/99-fbturbo.conf  /usr/share/X11/xorg.conf.d/99-fbturbo.conf
fi
#if test "$root_dev" = "/dev/mmcblk0p7";then
#sudo cp ./usr/cmdline.txt-noobs /boot/cmdline.txt
#else
#sudo cp ./usr/cmdline.txt /boot/
#fi
sudo cp ./usr/inittab /etc/
#sudo cp ./boot/config-32.txt /boot/config.txt
sudo touch ./.have_installed
echo "gpio:resistance:32:270:480:360" > ./.have_installed

sudo apt-get update
#FBCP install
wget --spider -q -o /dev/null --tries=1 -T 10 https://cmake.org/
if [ $? -eq 0 ]; then
#sudo apt-get update
sudo apt-get install cmake libraspberrypi-dev -y 2> error_output.txt
result=`cat ./error_output.txt`
echo -e "\033[31m$result\033[0m"
grep -q "^E:" ./error_output.txt
type cmake > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo rm -rf rpi-fbcp
wget --spider -q -o /dev/null --tries=1 -T 10 https://github.com
if [ $? -eq 0 ]; then
sudo git clone https://github.com/tasanakorn/rpi-fbcp
if [ $? -ne 0 ]; then
echo "download fbcp failed, copy native fbcp!!!"
sudo cp -r ./usr/rpi-fbcp .
fi
else
echo "bad network, copy native fbcp!!!"
sudo cp -r ./usr/rpi-fbcp .
fi
sudo mkdir ./rpi-fbcp/build
cd ./rpi-fbcp/build/
sudo cmake ..
sudo make
sudo install fbcp /usr/local/bin/fbcp
cd - > /dev/null
type fbcp > /dev/null 2>&1
if [ $? -eq 0 ]; then
if [[ "$deb_version" < "12.1" ]]; then
sudo cp -rf ./usr/99-fbturbo-fbcp.conf  /usr/share/X11/xorg.conf.d/99-fbturbo.conf
fi
sudo cp -rf ./etc/rc.local /etc/rc.local
fi
else
echo "install cmake error!!!!"
fi
else
echo "bad network, can't install cmake!!!"
fi

#evdev install
#nodeplatform=`uname -n`
#kernel=`uname -r`
version=`uname -v`
#if test "$nodeplatform" = "raspberrypi";then
#echo "this is raspberrypi kernel"
input_result=0
version=${version##* }
#version=${version#*#}
echo $version
if test $version -lt 2017;then
echo "reboot"
else
echo "need to update touch configuration"
wget --spider -q -o /dev/null --tries=1 -T 10 http://mirrors.zju.edu.cn/raspbian/raspbian
if [ $? -ne 0 ]; then
input_result=1
else
sudo apt-get install xserver-xorg-input-evdev  2> error_output.txt
dpkg -l | grep xserver-xorg-input-evdev > /dev/null 2>&1
if [ $? -ne 0 ]; then
input_result=1
fi
fi
if [ $input_result -eq 1 ]; then 
if [ $hardware_arch -eq 32 ]; then
sudo dpkg -i -B ./xserver-xorg-input-evdev_1%3a2.10.6-1+b1_armhf.deb 2> error_output.txt
elif [ $hardware_arch -eq 64 ]; then
sudo dpkg -i -B ./xserver-xorg-input-evdev_1%3a2.10.6-2_arm64.deb 2> error_output.txt
fi
fi
result=`cat ./error_output.txt`
echo -e "\033[31m$result\033[0m"
grep -q "error:" ./error_output.txt && exit
sudo cp -rf /usr/share/X11/xorg.conf.d/10-evdev.conf /usr/share/X11/xorg.conf.d/45-evdev.conf
#echo "reboot"
fi
#else
#echo "this is not raspberrypi kernel, no need to update touch configure, reboot"
#fi

sudo sync
sudo sync
sleep 1
if [ $# -eq 1 ]; then
sudo ./rotate.sh $1
elif [ $# -gt 1 ]; then
echo "Too many parameters"
fi

echo "reboot now"
sudo reboot
